home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / motif / oblur.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  280 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * oblur.c : openGL (motif) example showing how to use accumulation buffers
  25.  *           for motion blurs
  26.  *
  27.  * Author : Yusuf Attarwala
  28.  *          SGI - Applications
  29.  * Date   : Mar 93
  30.  *
  31.  *    note : the main intent of this program is to acc buffer functionality,
  32.  *           hence the rendering is kept simple (wireframe) 
  33.  *
  34.  *    press  left   button for animation
  35.  *    press  middle button to turn OFF motion blur (default)
  36.  *    press  right  button to turn ON  motion blur 
  37.  *
  38.  *
  39.  *---------------------------------------------------------------------------*/
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42.  
  43. #include <Xm/Xm.h> 
  44. #include <Xm/Frame.h>               /* for frame widgets */
  45. #include <Xm/Form.h>                /* for frame widgets */
  46. #include <X11/keysym.h>             /* keyboard translations */
  47. #include <X11/StringDefs.h>
  48.  
  49. #include <GL/gl.h>                  /* gl includes */
  50. #include <GL/glu.h>                 /* utility library includes */
  51. #include <GL/GLwMDrawA.h>      /* include for the drawing area widget */
  52.  
  53. /* function declarations */
  54.  
  55. static int doBlur = 0;
  56. void 
  57.     createToplevel(void),
  58.     drawScene(void),
  59.     setMatrix(void),
  60.     animation(void),
  61.     exposeCB(Widget,XtPointer,XtPointer),
  62.     resizeCB(Widget,XtPointer,XtPointer),
  63.     initCB(Widget,XtPointer,XtPointer),
  64.     inputCB(Widget,XtPointer,XtPointer);
  65.  
  66.  
  67. /* global variables */
  68.             
  69. Display       *display;             /* current display */
  70. XtAppContext  appContext;           /* X application context */
  71. float         ax,ay,az;             /* angles for animation */
  72. GLUquadricObj *quadObj;             /* used in drawscene */
  73.  
  74. Widget        toplevel,       /* toplevel shell */
  75.               glw;            /* current widget */
  76.              
  77. GLXContext    glxc;           /* current glx context */
  78.  
  79. Arg args[20];
  80. int acnt;
  81.  
  82. void 
  83. main(int argc, char** argv)
  84. {
  85.     XtToolkitInitialize();
  86.     appContext = XtCreateApplicationContext();
  87.     display    = XtOpenDisplay(appContext, NULL, "Oblur","oblur",NULL,0,
  88.                               &argc,argv);
  89.     if (!display) {
  90.         printf("%s : Unable to open display\n",argv[0]);
  91.         exit(0);
  92.     }
  93.  
  94.     printf("\n---------------------------------------------\n");
  95.     printf("OpenGL motion blur using acc buffer example \n\n");
  96.     printf("Press:  left   button for animation\n\
  97.         middle button to turn OFF motion blur (default)\n\
  98.         right  button to turn ON  motion blur \n");
  99.  
  100.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  101.     createToplevel();             /* create widget hierarchy */
  102.     XtAppMainLoop(appContext);    /* loop forever */
  103. }
  104.  
  105.  
  106. void
  107. createToplevel(void)
  108. {
  109.     Widget frame;
  110.  
  111.     acnt = 0;
  112.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  113.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  114.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  115.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  116.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  117.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  118.     toplevel  = XtAppCreateShell("openGL motionBlur","xtext",
  119.                                   applicationShellWidgetClass,
  120.                                   display,args,acnt);
  121.  
  122.     /* create a frame to hold glx widget */
  123.     frame   = XtVaCreateManagedWidget("frame", 
  124.                   xmFrameWidgetClass, toplevel, 
  125.                   NULL);
  126.  
  127.     /* create a double buffer widget, in rgb mode and manage it */
  128.     acnt = 0;
  129.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  130.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  131.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  132.     XtSetArg(args[acnt], GLwNaccumRedSize,       1); acnt++;
  133.     XtSetArg(args[acnt], GLwNaccumGreenSize,     1); acnt++;
  134.     XtSetArg(args[acnt], GLwNaccumBlueSize,      1); acnt++;
  135.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  136.     XtManageChild(glw);
  137.  
  138.     /* register callbacks */
  139.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  140.  
  141.     /* realize widget hierarchy */
  142.     XtRealizeWidget(toplevel);
  143.  
  144.     /* additional callbacks */
  145.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  146.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  147.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  148.  
  149. }
  150.  
  151. void
  152. drawScene(void)
  153. {
  154.     glClearColor(0.0, 0.0, 0.0, 0.0);
  155.     glClear(GL_COLOR_BUFFER_BIT);
  156.  
  157.     glPushMatrix();
  158.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  159.     glColor3f (1.0, 0.0, 0.0);
  160.     glRotatef (ax,1.0,0.0,0.0);
  161.     glRotatef (-ay,0.0, 1.0, 0.0);
  162.     gluCylinder (quadObj, 2.0,5.0,10.0,10,4);  /* draw a cone */
  163.     glPopMatrix();
  164.  
  165.     if (doBlur) {
  166.         glAccum(GL_MULT,0.33);
  167.         glAccum(GL_ACCUM,0.66);
  168.         glAccum(GL_RETURN,10.5);
  169.     }
  170.  
  171.     glFlush();
  172.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  173.  
  174. }
  175.  
  176. void
  177. setMatrix(void)
  178. {
  179.     glMatrixMode(GL_PROJECTION);
  180.     glLoadIdentity();
  181.     glOrtho(-15.0,15.0,-15.0,15.0,-15.0,15.0);
  182.     glMatrixMode(GL_MODELVIEW);
  183.     glLoadIdentity();
  184. }
  185.  
  186. void
  187. animation(void)
  188. {
  189.     register int i;
  190.     /* animate the cone */
  191.  
  192.     glClearAccum(0.0,0.0,0.0,0.0);
  193.     glClear(GL_ACCUM_BUFFER_BIT);
  194.     for (i=0;i<40;i++) {
  195.         ax += 15.0;
  196.         ay -= 12.0;
  197.         az += 15.0;
  198.         if (ax >= 360)  ax = 0.0;
  199.         if (ay <= -360) ay = 0.0;
  200.         if (az >= 360)  az = 0.0;
  201.         drawScene();
  202.     }
  203. }
  204.  
  205.  
  206. void 
  207. inputCB(Widget w, XtPointer client_data, XtPointer call)
  208. {
  209.     char buffer[1];
  210.     KeySym keysym;
  211.     GLwDrawingAreaCallbackStruct *call_data;
  212.  
  213.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  214.  
  215.     switch(call_data->event->type) {
  216.     case ButtonPress:
  217.         switch (call_data->event->xbutton.button) {
  218.         case Button1:
  219.         animation();
  220.             break;
  221.         case Button2 :
  222.         doBlur = 0;
  223.         fprintf(stdout,"Motion Blur DISABLED \n\n");
  224.         fflush(stdout);
  225.         drawScene();
  226.             break;
  227.         case Button3 :
  228.         doBlur = 1;
  229.         fprintf(stdout,"Motion Blur ENABLED \n\n");
  230.         fflush(stdout);
  231.         drawScene();
  232.             break;
  233.         }
  234.         break;
  235.     default:
  236.         break;
  237.     }
  238. }
  239.  
  240. void 
  241. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  242. {
  243.     GLwDrawingAreaCallbackStruct *call_data;
  244.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  245.  
  246.  
  247.     GLwDrawingAreaMakeCurrent(w, glxc);
  248.     glViewport(0, 0, call_data->width, call_data->height);
  249.     setMatrix();
  250.     drawScene();
  251. }
  252.  
  253. void 
  254. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  255. {
  256.     GLwDrawingAreaCallbackStruct *call_data;
  257.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  258.  
  259.  
  260.     GLwDrawingAreaMakeCurrent(w, glxc);
  261.     glViewport(0, 0, call_data->width, call_data->height);
  262.     drawScene();
  263. }
  264.  
  265. void
  266. initCB(Widget w, XtPointer client_data, XtPointer call)
  267. {
  268.     XVisualInfo *vi;
  269.  
  270.  
  271.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  272.     XtGetValues(w, args, 1);
  273.  
  274.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  275.  
  276.     ax = 10.0;
  277.     ay = -10.0;
  278.     az = 0.0;
  279. }
  280.